var path=require('path');
var assert=require('assert');
var request=require('supertest');
var app=require('../app.js');
var UserSchema=require('../lib/mongo').User;

var name1='test-1',name2='test-2';

describe('signup',()=>{

    describe('POST /signup',()=>{
        var agent=request.agent(app);

        afterEach(afterDone=>{
            UserSchema.remove({name:{$in:[name1,name2]}}).exec().then(()=>{
                afterDone();
            }).catch(afterDone);
        });

        it('wrong name',wrongNameDone=>{
            agent
                .post('/signup')
                .type('form')
                .attach('avatar',path.join(__dirname,'avatar.jpg'))
                .field({name:''})
                .redirects()
                .end((err,res)=>{
                    if(err)return wrongNameDone(err);
                    assert(res.text.match(/名字请限制在 1-10 个字符/));
                    wrongNameDone();
                });
        });

        it('wrong password',wrongNameDone=>{
            agent
                .post('/signup')
                .type('form')
                .attach('avatar',path.join(__dirname,'avatar.jpg'))
                .field({name:name1,password:'a'})
                .redirects()
                .end((err,res)=>{
                    if(err)return wrongNameDone(err);
                    assert(res.text.match(/密码至少 6 个字符/));
                    wrongNameDone();
                });
        });

        it('wrong repassword',wrongNameDone=>{
            agent
                .post('/signup')
                .type('form')
                .attach('avatar',path.join(__dirname,'avatar.jpg'))
                .field({name:name1,password:'123456',repassword:'111222'})
                .redirects()
                .end((err,res)=>{
                    if(err)return wrongNameDone(err);
                    assert(res.text.match(/两次输入密码不一致/));
                    wrongNameDone();
                });
        });

        it('wrong gender',wrongNameDone=>{
            agent
                .post('/signup')
                .type('form')
                .attach('avatar',path.join(__dirname,'avatar.jpg'))
                .field({name:name1,password:'123456',repassword:'123456',gender:'abc'})
                .redirects()
                .end((err,res)=>{
                    if(err)return wrongNameDone(err);
                    assert(res.text.match(/性别只能是 m、f 或 x/));
                    wrongNameDone();
                });
        });

        it('right avatar',wrongNameDone=>{
            agent
                .post('/signup')
                .type('form')
                .attach('avatar',path.join(__dirname,'avatar.jpg'))
                .field({name:name1,password:'123456',repassword:'123456',gender:'f',bio:name1})
                .redirects()
                .end((err)=>{
                    if(err)return wrongNameDone(err);
                    assert(true);
                    wrongNameDone();
                });
        });


    });

});